#variable decleration
	.data
vals: 	
	.space	4000
newLine:
	.asciiz "\n"
promptInput:
	.asciiz "Please enter a list of integers, press enter after each input.\nEnter 1111 to stop entering numbers.\n"
displaySorted:
	.asciiz "The sorted array is: \n"

#instructions
	.text
main:

	
li $t0, 0	#$t0 = n
li $t2, 1111	#$t2 = 1111, when input equals 1111 program stops reading input

#prompts user for input
li $v0, 4
la $a0, promptInput
syscall

#reads an integer from the console, stores integer in $t1
li $v0, 5
syscall
move $t1, $v0	#$t1 = in


li $t3, 0	#offset of array, incremented by 4 to get next element of array

While:
beq $t1, $t2, Continue

	
	sw $t1, vals($t3)	#stores the value of register t1 in vals[(value of register t3) / 4]

	addi $t3, $t3, 4	#increase offset of array
	addi $t0, $t0, 1	#increments n


	#read input and put it in register t1
	li $v0, 5
	syscall
	move $t1, $v0

	j While			#jump back to top of while loop


Continue:			#breaks out of the while loop

addi $t0 , $t0 , -1		#decrements n by 1 to give n-1
li $t1, 0			# i, counter for outer for loop of swap

SwapOuter:
bge $t1,$t0, ExitSwapOuter		
	li $t3, 0		# offset for A[j], incremented by 4 each iteration
	li $t4, 4		# j+1 for A[j+1], incremented by 4 each iteration
	li $t7, 0		# j, counter for inner for loop of swap
SwapInner:
	bge $t7, $t0, ExitSwapInner
		lw $t5, vals($t3)		#loads A[j] into $t5
		lw $t6, vals($t4)		#loads A[j+1] into $t6
		bge $t6, $t5, DontSwap		
			sw $t5, vals($t4)	#puts the value of A[j] in A[j+1]
			sw $t6, vals($t3)	#puts the value of A[j+1] in A[j]
		
		DontSwap: 		
		addi $t3,$t3,4			#increments offset of A[j]
		addi $t4,$t4,4			#increments offset of A[j+1]
		addi $t7, $t7, 1		#increments j by 1

		j SwapInner			#jump back to the top of the inner for loop

ExitSwapInner:
	addi $t1, $t1, 1			#increments i by 1
j SwapOuter					#jump back to the top of the outer for loop

ExitSwapOuter:					

#displays message to show the sorted array
li $v0, 4
la $a0, displaySorted
syscall

li $t1, 0					# i, counter for the for loop below
li $t2, 0					# offset of A[i], incremented by 4 each iteration
PrintLoop:

bgt $t1, $t0, ExitPrintLoop

	#print A[i]
	li $v0, 1
	lw $t3, vals($t2)
	move $a0, $t3
	syscall
	#print new line
	li $v0, 4
	la $a0, newLine
	syscall

	addi $t2, $t2, 4			#increments offset of the array
	addi $t1, $t1, 1			#increments i by 1
 	j PrintLoop				#jump back to the top of the print for loop

ExitPrintLoop:
